package paul.idtech.lostTraveller.model;

import java.util.Random;

import paul.idtech.lostTraveller.creatures.PlayerDefault;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

public class Game {

	// variables
	int roomCount;
	public PlayerDefault player;
	public DungeonRoom dr;
	private boolean gamelost;
	private boolean roomFinished;
	int lootedWeaponId;
	private Thread attackThread;
	private Thread gameThread;

	// constructor
	public Game(final Context context) {
		player = new PlayerDefault(context);
		dr = new DungeonRoom(context);
		gamelost = false;
		roomFinished = false;
		attackThread = new Thread() {
			public void run() {
				while (gamelost == false || roomFinished == false) {
					try {

						if (dr.creature.getEnergy() >= dr.creature.attackCost) {

							dr.creature.damageEnergyBy(dr.creature.attackCost);
							player.damageHealthBy(dr.creature.getEquippedItem()
									.getDamage());

						}

					} catch (Exception e) {

					}
				}
			}

		};
		

		gameThread = new Thread() {

			public void run() {

				while (gamelost == false) {
					try {

						if(player.checkAlive()==false){
							gamelost = true;
						}
						
						if (dr.creature.checkAlive() == false) {
							roomFinished = true;

							Random randLoot = new Random();
							lootedWeaponId = randLoot.nextInt() % 3 + 3;
							for (int i = 0; i < player.getInvSize(); i++) {
								int takenInvSlots = 0;
								if (player.getInvItem(i) != null) {
									takenInvSlots++;

								} else if (takenInvSlots == player.getInvSize()) {

									// opens inventory and asks to replace an
									// item

								} else if (player.getInvItem(i) == null) {

									player.setInvItem(i, lootedWeaponId);

								}

							}
							dr = new DungeonRoom(context);
							attackThread.start();
						}

					}

					catch (Exception e) {
						Log.e("htgrwhtrfe", "program crashed", e);
					}
				}

			}

		};
		gameThread.start();
		attackThread.start();
		
		
	}

	// methods

	// returns player image
	public Bitmap getPlayerImage() {

		return player.creatureImage;

	}

	// returns creature image
	public Bitmap getCreatureImage() {

		return dr.getCreatureImage();

	}

}
